package com.tanhua.app.controller;

import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.app.service.UserService;
import com.tanhua.app.service.UsersService;
import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UsersService usersService;

    @Autowired
    private UserService userService;


    //用户资料 - 读取
    //GET /users
    @GetMapping
    //public ResponseEntity findUserInfo(Long userID,
    //                                   @RequestHeader("Authorization") String token) {
    public ResponseEntity findUserInfo(Long userID) {
        ////1 校验用户是否登录
        //boolean verifyToken = JwtUtils.verifyToken(token);
        //if (!verifyToken) {
        //    return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(null);
        //}
        //
        ////2 解析Token  获取用户主键id
        //Claims claims = JwtUtils.getClaims(token);
        //Long id = Long.parseLong(claims.get("id").toString());

        if (userID == null) {
            userID = UserHolder.getUserId();
        }

        //3 调用Service实现功能
        UserInfoVo vo = usersService.findUserInfo(userID);

        //4 返回结果
        return ResponseEntity.ok(vo);
    }

    //用户资料 - 修改
    //PUT /users
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo,
                                         @RequestHeader("Authorization") String token) {
        //1 校验用户是否登录
        boolean verifyToken = JwtUtils.verifyToken(token);
        if (!verifyToken) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(null);
        }

        //2 解析Token  获取用户主键id
        Claims claims = JwtUtils.getClaims(token);
        Long id = Long.parseLong(claims.get("id").toString());

        //3 把id设置到UserInfo中
        userInfo.setId(id);

        //4 调用Service实现业务逻辑
        usersService.updateUserInfo(userInfo);

        //5 返回结果
        return ResponseEntity.ok(null);
    }

    //用户通用设置 - 读取
    //GET /users/settings
    @GetMapping("/settings")
    public ResponseEntity findSettings() {
        //1. 调用Service实现业务逻辑
        SettingsVo vo = usersService.findSettings();

        //2. 返回结果
        return ResponseEntity.ok(vo);
    }

    //设置陌生人问题 - 保存
    //POST /users/questions
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map) {
        //1 获取参数  就是问题
        String content = (String) map.get("content");

        //1. 调用Service实现业务逻辑
        usersService.questions(content);

        //2. 返回结果
        return ResponseEntity.ok(null);
    }

    //通知设置 - 保存
    //POST /users/notifications/setting
    @PostMapping("/notifications/setting")
    public ResponseEntity notifications(@RequestBody Map param) {
        //1. 调用Service实现业务逻辑
        usersService.notifications(param);

        //2. 返回结果
        return ResponseEntity.ok(null);
    }

    //黑名单 - 翻页列表
    //GET /users/blacklist
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        //1. 调用Service实现业务逻辑
        PageResult pageResult = usersService.blacklist(page, pagesize);

        //2. 返回结果
        return ResponseEntity.ok(pageResult);
    }

    //黑名单 - 移除
    //DELETE /users/blacklist/:uid
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlacklist(@PathVariable("uid") Long uid) {
        //1. 调用Service实现业务逻辑
        usersService.deleteBlacklist(uid);

        //2. 返回结果
        return ResponseEntity.ok(null);
    }

    //修改手机号- 1 发送短信验证码
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() throws Exception {
        //1.获取验证码
        String phone = UserHolder.getMobile();
        //2.调用service层方法校验验证码
        usersService.login(phone);
        //3.返回结果
        return ResponseEntity.ok(null);
    }


    //修改手机号 - 2 校验验证码
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity loginVerification(@RequestBody Map param) {
        //1.获取验证码
        String verificationCode = (String) param.get("verificationCode");
        //2 调用Service执行操作
        Boolean verification = usersService.yanzhengma(verificationCode);
        //3 返回结果
        Map retMap = new HashMap();
        retMap.put("verification", verification);
        return ResponseEntity.ok(retMap);

    }

    @PostMapping("/phone")
    public ResponseEntity updatePhone(@RequestBody Map param) {
        //1.获取手机号
        String phone = (String) param.get("phone");
        //2.调用service层修改用户手机号
        usersService.update(phone);
        //3.返回结果4

        return ResponseEntity.ok(null);
    }

    @PostMapping("/header")
    public ResponseEntity header(MultipartFile headPhoto) throws Exception {
        usersService.updateHeader(headPhoto);
        return ResponseEntity.ok(null);
    }

    @GetMapping("/counts")
    public ResponseEntity counts() {
        Map map = userService.counts();
        return ResponseEntity.ok(map);
    }
}
